import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreOplatformConfigService extends BaseService<any> {
  private readonly logger = new Logger(CoreOplatformConfigService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getConfig
   * 对应 PHP: CoreOplatformConfigService_core::getConfig()
   * 逻辑类型: undefined - undefined
   */
  async getConfig() {
    // 基于PHP真实逻辑: getConfig
    // PHP原文: $info = (new CoreConfigService())->getConfigValue(0, ConfigKeyDict::WXOPLATFORM);         return [             'app_id'            => $info['app_id'] ...
const info = this.coreConfigService.getConfigValue(0, ConfigKeyDict.WXOPLATFORM];
        return [
            app_id: info.app_id || '',// 开放平台账号的 appid
            app_secret: info.app_secret || '',// 开放平台账号的 secret
            token: info.token || '', // 开放平台账号的 token
            aes_key: info.aes_key || '',// 明文模式请勿填写 EncodingAESKey
            develop_app_id: info.develop_app_id || '',
            develop_upload_private_key: info.develop_upload_private_key || ''
        );
    }

    /**
     * 设置微信公众号配置
     * @param data
     * @return SysConfig|bool|Model
     */
    async setConfig(data){
        const config = [
            app_id: data.app_id || '',// 开放平台账号的 appid
            app_secret: data.app_secret || '',// 开放平台账号的 secret
            token: data.token || '', // 开放平台账号的 token
            aes_key: data.aes_key || '',// 明文模式请勿填写 EncodingAESKey
            develop_app_id: data.develop_app_id || '',
            develop_upload_private_key: data.develop_upload_private_key || ''
        ];
        return this.coreConfigService.setConfig(0, ConfigKeyDict.WXOPLATFORM, config);
    }


    /**
     *查询微信需要的静态信息
     * @return */
    async getStaticInfo(){
        const wap_domain = this.coreSysConfigService.getSceneDomain(0).wap_url || '';

        return [
            auth_serve_url: (string)url('/adminapi/wxoplatform/server',[),'', true), // 授权事件接收配置
            message_serve_url: (string)url('/adminapi/wxoplatform/message/APPID$', [),'',true), // 消息与事件接收配置
            auth_launch_domain: parse_url(request().domain()).host || '', // 授权发起页域名
            wechat_auth_domain: parse_url(wap_domain).host || '', // 公众号开发域名
            upload_ip: gethostbyname('oss.niucloud.com')
        ;
  }
  }

  /**
   * setConfig
   * 对应 PHP: CoreOplatformConfigService_core::setConfig()
   * 逻辑类型: undefined - undefined
   */
  async setConfig(data: any[]) {
    // 基于PHP真实逻辑: setConfig
    // PHP原文: $config = [             'app_id'            => $data['app_id'] ?? '',// 开放平台账号的 appid             'app_secret'        => $data['app_secret'] ?? '',// ...
const config = [
            app_id: data.app_id || '',// 开放平台账号的 appid
            app_secret: data.app_secret || '',// 开放平台账号的 secret
            token: data.token || '', // 开放平台账号的 token
            aes_key: data.aes_key || '',// 明文模式请勿填写 EncodingAESKey
            develop_app_id: data.develop_app_id || '',
            develop_upload_private_key: data.develop_upload_private_key || ''
        ];
        return this.coreConfigService.setConfig(0, ConfigKeyDict.WXOPLATFORM, config);
    }


    /**
     *查询微信需要的静态信息
     * @return */
    async getStaticInfo(){
        const wap_domain = this.coreSysConfigService.getSceneDomain(0).wap_url || '';

        return [
            auth_serve_url: (string)url('/adminapi/wxoplatform/server',[),'', true), // 授权事件接收配置
            message_serve_url: (string)url('/adminapi/wxoplatform/message/APPID$', [),'',true), // 消息与事件接收配置
            auth_launch_domain: parse_url(request().domain()).host || '', // 授权发起页域名
            wechat_auth_domain: parse_url(wap_domain).host || '', // 公众号开发域名
            upload_ip: gethostbyname('oss.niucloud.com')
        ;
  }
  }

  /**
   * getStaticInfo
   * 对应 PHP: CoreOplatformConfigService_core::getStaticInfo()
   * 逻辑类型: undefined - undefined
   */
  async getStaticInfo() {
    // 基于PHP真实逻辑: getStaticInfo
    // PHP原文: $wap_domain = (new CoreSysConfigService())->getSceneDomain(0)['wap_url'] ?? '';          return [             'auth_serve_url' => (string)url('/admina...
const wap_domain = this.coreSysConfigService.getSceneDomain(0).wap_url || '';

        return [
            auth_serve_url: (string)url('/adminapi/wxoplatform/server',[),'', true), // 授权事件接收配置
            message_serve_url: (string)url('/adminapi/wxoplatform/message/APPID$', [),'',true), // 消息与事件接收配置
            auth_launch_domain: parse_url(request().domain()).host || '', // 授权发起页域名
            wechat_auth_domain: parse_url(wap_domain).host || '', // 公众号开发域名
            upload_ip: gethostbyname('oss.niucloud.com')
        ;
  }
  }
}
